'use strict'

define(['jquery', 'bootstrap', 'select2', 'shiftform', 'validate', 'validate.en', 'datetimepicker' ], function($) {
  return {
    init : function(){
    
      this.initPage();
      this.initSearch();
    },
    
    initPage : function(){
      
      var that = this;
      $(document).on("hidden.bs.modal" , "#edit" ,function(e){
          $(document).off("click",".add-row")
      });
          
      $(document).on("shown.bs.modal" , "#edit" ,function(e){
        
        $('#fee-search').on("click", function(e){
          $("select.form-control").trigger("blur");
          that._getAjaxData();
        })
      
        var $form = $(e.currentTarget).find("form")                          //bind shiftform
        $form.shiftform({
          beforeSubmit : function(el) {
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            $form.trigger("validate.ajax.submit");
            return $.bt_validate.result[$.bt_validate.form_id];
          }
        })
        
        $(document).on("click", '.delete-row', function(e) {
          e.preventDefault();
          $(".tyre-fee tbody tr").each(function() {
            var min = parseInt(0);
            if($(this).prev().length > 0) {
                min = parseInt($(this).prev().find("input[name='rangeFrom']").val()) + 1;
            }
            $(this).find("input[name='rangeFrom']").data("validate", "required|float|min,"+parseInt(min)+"|max_decimal_digit,0");
            if($(this).find("input[type='checkbox']").prop('checked')) {
             $(this).remove();
            }
          })

        });
        
        
        
        $(document).on("click", '.add-row', function(e) {
            e.preventDefault();
          
            var $lastInput = $(".tyre-fee tbody input[name='rangeFrom']").last();
            console.log($lastInput);
            if(!$lastInput.val()) {
               $lastInput.focus();
            } else {
              var min = parseInt($lastInput.val()) + 1;
              var html = '<tr>' +
                           '<td style="text-align:center;"><input type="checkbox" name="tyreFee"></td>'+
                           '<td style="text-align:center;"><input class="form-control text-center" type="number" name="rangeFrom" value="" data-validate="required|float|min,'+min+'|max_decimal_digit,0" /></td>'+
                           '<td style="text-align:center;"><input class="form-control text-center" type="number" name="price" value="" data-validate="required|float|min,0|max_decimal_digit,2" step="0.01" /></td>'+
                         '</tr>';
              var $lastInput = $(".tyre-fee tbody").append(html);
            }
            
        });
          
      });
      
    },
    
    _getAjaxData: function() {
      
      if($("#searchOrgId").length > 0){
         var orgId = $('select[name="orgId"]').val().trim();
        }
        else{
           var orgId = $('input[name="orgId"]').val();
        }
       
        var powerTypeId = $('select[name="powerTypeId"]').val().trim();
        
        if((orgId != "" && orgId != undefined && orgId != null) && (powerTypeId != "" && powerTypeId != undefined && powerTypeId != null)) {
            
          $.ajax({
                url:window.__CTX__.root + "/tyre-fees?prepare=true&orgId="+orgId + "&powerTypeId="+powerTypeId,
                type:"GET",
            }).done(function(data){
                data = typeof data === 'String' ? JSON.parse(data) : data
                if(parseInt(data.code) == 0) {  
                    
                    var response = data.data;
                    if(response.tyreFees.length > 0) {
                        $("#edit tbody tr").remove();
                        $.each(response.tyreFees, function(key, val){
                           if(key == 0) {
                             var html = '<tr>' +
                               '<td style="text-align:center;"><input type="checkbox" name="tyreFee" disabled></td>'+
                               '<td style="text-align:center;"><input class="form-control text-center first-rangefrom" type="number" name="rangeFrom" value="0" data-validate="required|float|min,0" readonly /></td>'+
                               '<td style="text-align:center;"><input class="form-control text-center first-price" type="number" name="price" value="'+ response.tyreFees[key].price +'" data-validate="required|float|min,0|max_decimal_digit,2" step="0.01" /></td>'+
                               '</tr>'
                               $("#edit tbody").append(html)
                           } else {
                           var min = parseInt(response.tyreFees[key-1].rangeFrom) + 1;
                             var html = '<tr>' +
                               '<td style="text-align:center;"><input type="checkbox" name="tyreFee"></td>'+
                               '<td style="text-align:center;"><input class="form-control text-center" type="number" name="rangeFrom" value="'+ response.tyreFees[key].rangeFrom +'" data-validate="required|float|min,'+min+'" /></td>'+
                               '<td style="text-align:center;"><input class="form-control text-center" type="number" name="price" value="'+ response.tyreFees[key].price +'" data-validate="required|float|min,0|max_decimal_digit,2" step="0.01" /></td>'+
                               '</tr>'
                             $("#edit tbody").append(html)
                           }
                        });
                    } else {
                        $("#edit tbody tr").remove();
                        var html = '<tr>' +
                        '<td style="text-align:center;"><input type="checkbox" name="repairFee" disabled></td>'+
                        '<td style="text-align:center;"><input class="form-control text-center first-rangefrom" type="number" name="rangeFrom" value="0" data-validate="required|float|min,-1" readonly /></td>'+
                        '<td style="text-align:center;"><input class="form-control text-center first-price" type="number" name="price" value="0.0" data-validate="required|float|min,0|max_decimal_digit,2" step="0.01" /></td>'+
                        '</tr>'
                        $("#edit tbody").append(html)
                        //$("#edit tbody").append('<tr><td class="text-center" colspan="3">有0条数据</td></tr>')
                    }

                } else {
                    console.log(data.code);
//                    changeBackdropMessage('查询失败', 'text-danger', options.failure, data.data.url)
                }
                
            }).fail(function() {
                console.log(data.code);
//              changeBackdropMessage('查询失败', 'text-danger', data.url)
            })
            $("form button").removeClass("disabled")
        }
        
    },
    
    initSearch : function(){
          
      $(document).on("shown.bs.modal" , "#search" ,function(e){
          $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
      });
          
    },
  }
})

